<?php

namespace App\Models\Admin;

use App\Log\GoodsStandardAdminLog;
use Illuminate\Database\Eloquent\Model;

class GoodsStandard extends Base
{
    const OPERATE_PID = 2;
    const MODEL = 'goods_standard';

    protected $table = 'goods_standard';
    protected $dateFormat = 'U';
    public $timestamps = true;
    protected $guarded = [];

    public function category()
    {
        return $this->hasOne(GoodsCategory::class,'id','category_id');
    }

    public function category_p()
    {
        return $this->hasOne(GoodsCategory::class,'id','pid');
    }

    public function spu()
    {
        return $this->hasMany(GoodsStandardSpu::class,'standard_goods_id','id');
    }

    public function goods()
    {
        return $this->hasOne(Goods::class,'standard_goods_id','id');
    }

    public function brandInfo()
    {
        return $this->hasOne(GoodsDatum::class,'id','brand');
    }

    public function standardName()
    {
        return $this->hasMany(GoodsStandardName::class,'goods_standard_id','id');
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new GoodsStandardAdminLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
